import pygame

from Config import SHOTMUSIC,PLAYERLV
import Sprite_Group
import Bullets
import Musics
import  StatusBars
from Skins import player_skin_1
from My_functions import move

class Player(pygame.sprite.Sprite):

    def __init__(self,x,y):
        super().__init__()
        #移动图像
        self.images = player_skin_1
        self.bulletskin = player_skin_1['Bullet']
        #初始化方向
        self.direction='U'
        #初始化图像
        self.current_frame=0
        self.frame_rate=6
        self.current_rate=0
        self.image = self.images[self.direction][self.current_frame]
        #获取图像参数
        self.rect=self.image.get_rect()
        #为图像参数赋值
        self.rect.x=x
        self.rect.y=y
        #等级
        self.level=1
        self.exp=0
        self.max_exp=PLAYERLV[self.level][1]
        #初始速度
        self.move_speed=PLAYERLV[self.level][2]
        #射速
        self.shot_speed=PLAYERLV[self.level][3]
        #控制射速
        self.last_time=0
        #是否存活
        self.max_hp=PLAYERLV[self.level][0]
        self.hp = self.max_hp
        self.live=True

    def shot(self):
        now_time=pygame.time.get_ticks()
        music=Musics.Music(SHOTMUSIC)
        if now_time-self.last_time>=self.shot_speed:
            self.last_time=now_time
            b = Bullets.PlayerBullet(self)
            Sprite_Group.Player_Bullet_group.add(b)
            music.play()

    def is_live(self):
        if self.hp<=0:self.live=False

    def uplv(self):
        # 是否存活
        self.max_hp = PLAYERLV[self.level][0]
        self.hp = self.max_hp
        self.max_exp = PLAYERLV[self.level][1]
        # 初始速度
        self.move_speed = PLAYERLV[self.level][2]
        # 射速
        self.shot_speed = PLAYERLV[self.level][3]
        # 控制射速
        self.last_time = 0

    def is_uplv(self):
        if self.exp >= self.max_exp:
            self.exp -=self.max_exp
            self.level +=1
            self.uplv()

    def update(self,keys,mouse,*args, **kwargs):
        if keys[pygame.K_LEFT]:move(self, self.move_speed,'L')
        elif keys[pygame.K_RIGHT]:move(self, self.move_speed,'R')
        elif keys[pygame.K_UP]:move(self, self.move_speed,'U')
        elif keys[pygame.K_DOWN]:move(self, self.move_speed, 'D')
        if mouse[0]:self.shot()
        self.is_live()
        self.is_uplv()
        self.current_rate+=1
        if self.current_rate>self.frame_rate:
            self.current_frame = (self.current_frame + 1) % len(self.images[self.direction])
            self.current_rate=0

class Player1(Player):
    def __init__(self,x,y):
        super().__init__(x,y)
        self.name = "Player1"
        healthbar = StatusBars.Player1StatusBar(self, 200, 70, 0, 0)
        Sprite_Group.StatusBar_grop.add(healthbar)

    def update(self,keys,mouse,*args, **kwargs):
        if keys[pygame.K_a]:move(self, self.move_speed,'L')
        elif keys[pygame.K_d]:move(self, self.move_speed,'R')
        elif keys[pygame.K_w]:move(self, self.move_speed,'U')
        elif keys[pygame.K_s]:move(self, self.move_speed, 'D')
        if keys[pygame.K_SPACE]:self.shot()
        self.is_live()
        self.is_uplv()
        self.current_rate += 1
        if self.current_rate > self.frame_rate:
            self.current_frame = (self.current_frame + 1) % len(self.images[self.direction])
            self.current_rate = 0

class Player2(Player):
    def __init__(self,x,y):
        super().__init__(x,y)
        self.name = "Player2"
        healthbar = StatusBars.Player2StatusBar(self, 200, 70, 0, 0)
        Sprite_Group.StatusBar_grop.add(healthbar)
    pass